package com.example.sandabackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.sandabackend.entity.User;
import com.example.sandabackend.service.UserService;
import com.example.sandabackend.mapper.UserMapper;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 不过青丝白发
 * @description 针对表【sys_name】的数据库操作Service实现
 * @createDate 2023-04-05 15:22:53
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Autowired
    private UserMapper userMapper;

    //查询语句
    public List<User> findAll() {
        return userMapper.findAll();
    }

    //根据id删除数据（真实删除）
    @Override
    public Integer delectById(Integer id) {
        return userMapper.delete(id);
    }

    //手动分页查询
    @Override
    public Map findPage(Integer pageNum, Integer pageSize, String username, String email, String address) {
        pageNum = (pageNum - 1) * pageSize;
        List<User> data = userMapper.findPage(pageNum, pageSize);
        Integer total = userMapper.countTotal();
        Map res = new HashMap();
        res.put("data", data);
        res.put("total", total);

        return res;
    }

    //分页查询 mybatis提供方法
    public IPage<User> findPageByCondition(Integer pageNum, Integer pageSize,
                                           String username,
                                           String email,
                                           String address) {
        IPage<User> page = new Page<>(pageNum, pageSize);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (!"".equals(username)) {
            queryWrapper.like("username", username);
        }
        if (!"".equals(email)) {
            queryWrapper.like("email", email);
        }
        if (!"".equals(address)) {
            queryWrapper.like("address", address);
        }
        return page(page, queryWrapper);
    }


//   // 插入和修改
//    @Override
//    public int save(User user) {
//        if(user.getId()==null){//若无id号则插入新数据
//            return userMapper.insert(user);
//        }else{//若有id号则修改
//            return userMapper.update(user);
//        }
//    }
    //mybatis-plus 自带方法
    public boolean saveUser(User user) {
        return saveOrUpdate(user);
    }
}




